// 导入数据库API
const db = require('../../config/db');

// 操作数据库的业务
module.exports = {
    // 示例：
    test() {
        return new Promise((resolve, reject) => { })
    },
 
    // 删除考试（用户自己删除）
    remove(topic_lib_id, user_id) {
        return new Promise((resolve, reject) => {
            let sql = `delete from  db_exam where topic_lib_id = "${topic_lib_id}" and user_id="${user_id}"`
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: '删除考试失败。'
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: '删除考试成功。'
                    })
                }
            });
        })
    },

    // 删除考试（管理员删除）
    delete(topic_lib_id) {
        return new Promise((resolve, reject) => {
            let del_exam_sql = `delete from  db_exam where topic_lib_id = "${topic_lib_id}"`
            db.query(del_exam_sql, (err, rows) => {
                if (err) {reject({code: -1,msg: '删除考试失败。'})} else {resolve({code: 200, msg: '删除考试成功。'})}
            });
        })
    },

    // 考试列表
    list(page_num, page_size) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let len = page_size;
            let sql = `select * from db_exam limit ${startIndex},${len}`;
            db.query(sql, (err, rows) => {
                // console.log({err})
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取考试列表失败。',
                        err
                    })
                } else {
                    
                    resolve({
                        code: 200,
                        msg: "获取考试列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        }
                    })
                }
            })
        })

    },

    list2(user_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_exam where user_id = "${user_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取考试列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取考试列表成功。",
                        result: rows
                    })
                }
            })
        })

    },

    list3(page_num, page_size) {
        // console.log(page_num, page_size,user_id)
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let len = page_size;
            let sql = `select * from db_exam limit ${startIndex},${len}`;
            db.query(sql, (err, rows) => {
                // console.log({err})
                if (err) {
                    resolve({
                        code: -1,
                        msg: '获取考试列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取考试列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        }
                    })
                }
            })
        })

    },

    // 考试详情
    detail(topic_lib_id) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_exam where topic_lib_id="${topic_lib_id}"`;
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询考试列表失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "查询考试详情成功。",
                        result: rows
                    })
                }
            })
        })
    },

    // 搜索考试
    // 参数：keyword
    search(keyword) {
        return new Promise((resolve, reject) => {
            let sql = "select * from db_exam where exam_name LIKE CONCAT('%" + keyword + "%') or exam_type LIKE CONCAT('%" + keyword + "%')";
            db.query(sql, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询考试信息失败。',
                        err
                    })
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: 200,
                            msg: '没有该关键字匹配考试信息。',
                            result:[]
                        })
                    } else {
                        resolve({
                            code: 200,
                            msg: '查询考试信息成功。',
                            result: rows
                        })
                    }
                }
            })
        })
    },

    // 统计考试数量
    total() {
        return new Promise((resolve, reject) => {
            db.query('select count(*) as total from db_exam', (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计考试数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取考试数量成功。",
                        total: rows[0]['total']
                    })
                }
            })

        })
    },

    total3(user_id) {
        return new Promise((resolve, reject) => {
            db.query(`select exam_name from db_exam where user_id = "${user_id}"`, (err, rows) => {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '统计考试数量失败。',
                        err
                    })
                } else {
                    resolve({
                        code: 200,
                        msg: "获取考试数量成功。",
                        result: rows
                    })
                }
            })

        })
    },
 
}